var element,layer,upload,form,laydate;
var imgAddress;

$(document).click(function(){//子页面
    $(".rightmenu",window.parent.document).hide();
});

layui.use(["jquery", "upload", "form", "layer", "element","laydate","layedit"], function () {
    $ = layui.$,
        element = layui.element,
        layer = layui.layer,
        upload = layui.upload,
        form = layui.form,
        laydate = layui.laydate;
    laydate.render({
        elem: "#startDate",
        type: "datetime",
        trigger: "click"
    });
    laydate.render({
        elem: "#expireDate",
        type: "datetime",
        trigger: "click"
    });

    //上传图片
    upload.render({
        elem:"#test1",
        url:"/Upload/upload",
        method:"post",
        field:"file",
        accept:"file",
        done:function(res, index, upload){
            if (res.data != null) {
                imgAddress = res.data.substring(res.data);
                localStorage.setItem("imgSrc",imgAddress);
                $(".uploadHeadImage img").css("display","inline");
                $(".uploadHeadImage img").attr("src",imgAddress);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失败");
            }
        }
    });

    //获取轮播图数据
    var advertisementData = JSON.parse(localStorage.getItem("advertisementData"));
    $("#imgTitle").val(advertisementData.imgTitle);
    if (advertisementData.imgType == "视频"){
        $("#imgType").val("1");
    } else if (advertisementData.imgType == "商品"){
        $("#imgType").val("2");
    }
    form.render();
    $("#startDate").val(advertisementData.startDate);
    $("#expireDate").val(advertisementData.expireDate);
    $("#remarks").val(advertisementData.remarks);
    $("#carouselId").val(advertisementData.carouselId);
    $(".uploadHeadImage img").css("display","inline");
    $(".uploadHeadImage img").attr("src",advertisementData.imgAddress);
});

//提交修改轮播图信息
$("#sub").on("click",function () {
    var imgTitle = $("#imgTitle").val();
    var imgType =  $("#imgType").val();
    if (imgType == "0"){
        imgType = '';
    }
    if (imgType == "1"){
        imgType = "视频";
    }
    if (imgType == "2"){
        imgType = "商品";
    }
    var startDate = $("#startDate").val();
    var expireDate = $("#expireDate").val();
    var remarks = $("#remarks").val();
    var carouselId = $("#carouselId").val();
    var data = {
        imgTitle: imgTitle,
        imgType: imgType,
        startDate: startDate,
        expireDate: expireDate,
        remarks: remarks,
        imgAddress: imgAddress,
        carouselId: carouselId
    }
    $.ajax({
        url:'/Advertisement/upAdvertisement',
        methods: "post",
        dataType:'json',
        data: data,
        success:function (data) {
            if (data.data){
                layer.msg("修改成功");
                location.href = "/advertisement/advertisement_list.html";
            }else{
                layer.msg("修改失败");
            }
        }
    });
    //阻止表单提交刷新页面
    return false;
});

//返回
function backOff() {
    //返回并刷新页面
    location.href = "/Advertisement/advertisement_list.html";
}